草庐IT

pointers - 戈朗 : interface func to print memory address

全部标签

pointers - 使用 xlsx 包 panic : runtime error: invalid memory address or nil pointer dereference Go

var(file*xlsx.Filesheet*xlsx.Sheetrow*xlsx.Rowcell*xlsx.Cell)funcaddValue(valstring){cell=row.AddCell()cell.Value=val}并从http://github.com/tealeg/xlsx导入当控制权到达这条线时cell=row.AddCell()这是panic。错误:panic:runtimeerror:invalidmemoryaddressornilpointerdereference有人可以建议这里出了什么问题吗? 最佳答案

戈朗 : strange issue with coroutines and channels

我写了一个测试代码,但不明白为什么会得到这个结果。我的sub()应该根据channel值更新或返回countersend1=counter++send0=returncounter我启动了10个go例程con()。他们应该简单地发送许多1到channel(这个增加计数器)我等待1秒并将0发送到channel。我应该获得什么值(value)?我想首先,我得到一个“随机”值,但我得到100000(好的10x10000比1秒快)现在我变了fori:=0;i到fori:=0;i现在我的返回值是1为什么!?现在取消注释main()中的fmt.Println(counter)。如您所见,计数器工作

go - 无法调用 unsafe.Pointer(指向 C 函数)作为函数

我正在尝试调用具有相同签名的C函数,它们采用2个int参数。并且这个错误cannotcallnon-functionf(typeunsafe.Pointer)在编译时出现。packagemain/*intadd(inta,intb){returna+b;}intsub(inta,intb){returna-b;}*/import"C"import("fmt""unsafe")funcmain(){a:=C.int(1)b:=C.int(2)fx:=make([]unsafe.Pointer,2)fx[0]=C.addfx[1]=C.subfor_,f:=rangefx{fmt.Prin

xml - 戈朗 :xml unmarshall not working as expected

获取xml格式的响应:https://sites.google.com/feeds/activity/site/siteName2009-09-10T05:24:23.120ZActivityGoogleSites1https://sites.google.com/feeds/activity/site/siteName/9403759969528760622009-09-10T03:38:42.585ZhomeUserdeletedhomeUseruser@gmail.comhttps://sites.google.com/feeds/activity/site/siteName/7

pointers - Go函数指针问题

这个问题在这里已经有了答案:MyobjectisnotupdatedevenifIusethepointertoatypetoupdateit(3个答案)GolangOperatorOverloading(1个回答)Golangchangingvaluesofastructinsideamethodofanotherstruct(2个答案)CopyinstancesoftypeT,whenanyofthemethodsofanamedtypeThaveapointerreceiver(1个回答)关闭5年前。我有一个结构typekeeperstruct{ptrint32}然后我给它添加一

pointers - Go 中的指针解除引用是如何工作的?

我正在http://tour.golang.org/学习golang教程,并在example29中尝试了一些东西为了方便大家引用,原例子复制在这里:packagemainimport"fmt"typeVertexstruct{X,Yint}var(p=Vertex{1,2}//hastypeVertexq=&Vertex{1,2}//hastype*Vertexr=Vertex{X:1}//Y:0isimplicits=Vertex{}//X:0andY:0)funcmain(){fmt.Println(p,q,r,s)}它非常基础,展示了如何创建这个奇特的新结构Vertex的实例。E

pointers - 指向方法参数中接口(interface)的指针?

Gistwithcode如何在第30行使用接口(interface)Herbivore代替*Mouse?我想将实现Herbivore接口(interface)的不同结构传递给方法eatingVictim,而不仅仅是Mouse 最佳答案 让我解释一下:首先在这个函数中:func(predatorCat)eatingVictim(victim*Mouse){fmt.Println(predator.name+"'seatingvictim"+victim.name)predator.hungry=falsevictim.alive=fa

戈朗 : command-line-arguments undefined: variable

我想在Golang中创建一个外部函数用于显示使用情况,但我不知道如何调用flag变量。这是我的实际代码:packagemainimport("flag""fmt""os")funcUsage(){ifArgSend{fmt.Printf("Usage:SEND")flag.PrintDefaults()os.Exit(0)}elseifArgTest{fmt.Printf("Usage:-test")flag.PrintDefaults()os.Exit(0)}elseifArgMacro{fmt.Printf("Usage:-macro")os.Exit(0)}else{fmt.Pr

pointers - 隐藏 nil 值,理解为什么 golang 在这里失败

我不明白如何正确确保某些东西不是nil在这种情况下:packagemaintypeshowerinterface{getWater()[]shower}typedisplaystruct{SubDisplay*display}func(ddisplay)getWater()[]shower{return[]shower{display{},d.SubDisplay}}funcmain(){//SubDisplaywillbeinitializedwithnulls:=display{}//water:=[]shower{nil}water:=s.getWater()for_,x:=ra

unit-testing - 戈朗 : tests and working directory

我正在用Go为我的应用程序编写一些单元测试。但是测试失败了,因为它找不到配置文件。通常,二进制文件会在路径conf/*.conf下的工作目录中查找配置文件。我想浏览到有conf/的目录,在里面运行gotest就可以解决,但是还是报文件系统找不到路径指定。如何告诉gotest使用某个目录作为工作目录,以便测试可以实际执行? 最佳答案 您可以使用Caller获取当前测试源文件的路径,如下所示:packagesampleimport("testing""runtime""fmt")funcTestGetFilename(t*testing